package com.yunyao.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.common.web.util.ConvertUtil;
import com.yunyao.common.web.util.PageResult;
import com.yunyao.common.web.util.StringUtils;
import com.yunyao.common.web.util.YResult;
import com.yunyao.dao.model.BamuPurchaseApplyMaterial;
import com.yunyao.dao.model.BamuSupplyApplyMaterial;
import com.yunyao.dao.model.BamuSupplyApplyMaterial;
import com.yunyao.dao.service.bamu.IBamuSupplyApplyMaterialService;
import com.yunyao.dao.service.bamu.IBamuSupplyApplyMaterialService;
import com.yunyao.framework.dto.supplyorder.SupplyMaterialSaveDto;
import com.yunyao.framework.service.SupplyMaterialService;
import com.yunyao.framework.vo.supplyorder.SupplyMaterialVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chenyejian
 * @date 2024-07-16
 */
@Slf4j
@Service
public class SupplyMaterialServiceImpl implements SupplyMaterialService {
    @Autowired
    private IBamuSupplyApplyMaterialService bamuSupplyApplyMaterialService;
    @Override
    public YResult<PageResult<SupplyMaterialVO>> pageList(String orderId,Long pageIndex,Long pageSize) {
        if (StringUtils.isEmpty(orderId)) {
            return YResult.success(new PageResult<>());
        }
        LambdaQueryWrapper<BamuSupplyApplyMaterial> queryWrapper = new LambdaQueryWrapper<BamuSupplyApplyMaterial>();
        queryWrapper.eq(BamuSupplyApplyMaterial::getPurchaseApplyId, orderId)
                .eq(BamuSupplyApplyMaterial::getIsDeleted, DataDeletedStatus.NO.getType())
                .orderByAsc(BamuSupplyApplyMaterial::getMaterialSortNum)
                .orderByAsc(BamuSupplyApplyMaterial::getIsCopy);
        IPage<BamuSupplyApplyMaterial> page = new Page<>(pageIndex, pageSize);
        page =bamuSupplyApplyMaterialService.page(page, queryWrapper);
        return YResult.success(new PageResult<>(convertDao2VoList(page.getRecords()),pageIndex,pageSize,page.getPages(),page.getTotal()));
    }


    /**
     * 供货清单计算已发货量生成材料列表
     * @param   orderId
     * @return
     */
    @Override
    public YResult<List<SupplyMaterialVO>> hasSupplyMaterialList(String orderId) {
        if (StringUtils.isEmpty(orderId)) {
            return YResult.success(new ArrayList<>());
        }
        /*
         * 供货订单材料数量
         */
        LambdaQueryWrapper<BamuSupplyApplyMaterial> queryWrapper = new LambdaQueryWrapper<BamuSupplyApplyMaterial>();
        queryWrapper.eq(BamuSupplyApplyMaterial::getPurchaseApplyId, orderId)
                .eq(BamuSupplyApplyMaterial::getIsDeleted, DataDeletedStatus.NO.getType())
                .orderByAsc(BamuSupplyApplyMaterial::getMaterialSortNum)
                .orderByAsc(BamuSupplyApplyMaterial::getIsCopy);
        List<BamuSupplyApplyMaterial> bamuSupplyApplyMaterialList = bamuSupplyApplyMaterialService.hasSupplyMaterialList(orderId);
        if (CollectionUtils.isEmpty(bamuSupplyApplyMaterialList)){
            return YResult.success(new ArrayList<>());
        }
//        List<BamuSupplyApplyMaterial> bamuSupplyApplyMaterialList = new ArrayList<>();
//        for (BamuSupplyApplyMaterial bamuPurchaseApplyMaterial : bamuSupplyApplyMaterialList){
//            if (ConvertUtil.getBigDecimalValue(bamuPurchaseApplyMaterial.getSupplyCountNum(),BigDecimal.ZERO).compareTo(ConvertUtil.getBigDecimalValue(bamuPurchaseApplyMaterial.getOrderCount(),BigDecimal.ZERO)) == 0){
//                continue;
//            }
//            else {
//                resultList.add(bamuPurchaseApplyMaterial);
//            }
//        }
//        if (CollectionUtils.isEmpty(resultList)){
//            return YResult.success(new ArrayList<>());
//        }
        return YResult.success((convertDao2VoList(bamuSupplyApplyMaterialList)));
    }








    @Override
    public YResult<String> saveOrUpdate(SupplyMaterialSaveDto saveDto) {
        try {
                BamuSupplyApplyMaterial bamuPurchaseApplyMaterial = convertDto2Dao(saveDto);
            bamuSupplyApplyMaterialService.updateById(bamuPurchaseApplyMaterial);
                return YResult.success(bamuPurchaseApplyMaterial.getId());
        }catch (Exception e){
            log.error("保存失败",e);
        }
        return YResult.error("保存失败");
    }

    @Override
    public YResult<String> deleteByIds(String[] ids) {
        try {
            bamuSupplyApplyMaterialService.removeByIds(Arrays.asList(ids));
            return YResult.success("删除成功");
        }catch (Exception e){
            log.error("删除失败",e);
        }
        return YResult.error("删除失败");
    }

    private List<SupplyMaterialVO> convertDao2VoList(List<BamuSupplyApplyMaterial> list){
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<SupplyMaterialVO> resultList = new ArrayList<>();
        for (BamuSupplyApplyMaterial applyMaterial : list){
            SupplyMaterialVO materialVO = convertDao2Vo(applyMaterial);
            resultList.add(materialVO);
        }
        return resultList;
    }

    private SupplyMaterialVO convertDao2Vo(BamuSupplyApplyMaterial bamuPurchaseApplyMaterial) {
        SupplyMaterialVO projectMaterialVO = new SupplyMaterialVO();
        BeanUtils.copyProperties(bamuPurchaseApplyMaterial,projectMaterialVO);
        return projectMaterialVO;
    }

    /**
     * saveDto转化为dao
     */
    private BamuSupplyApplyMaterial convertDto2Dao(SupplyMaterialSaveDto saveDto){
        BamuSupplyApplyMaterial bamuPurchaseApplyMaterial = new BamuSupplyApplyMaterial();
        BeanUtils.copyProperties(saveDto,bamuPurchaseApplyMaterial);
        return bamuPurchaseApplyMaterial;
    }


}
